
pkgname=openvpn
pkgver=2.4.6
pkgrel=1
pkgdesc="An easy-to-use, robust, and highly configurable VPN (Virtual Private Network)"
arch=('x86_64')
url="https://openvpn.net/index.php/open-source.html"
depends=('openssl' 'lzo2' 'iproute2')
license=('custom')
install=openvpn.install
backup=(usr/share/openvpn/easy-rsa/vars
        usr/share/openvpn/easy-rsa/openssl.cnf)
source=("https://swupdate.openvpn.net/community/releases/openvpn-${pkgver}.tar.xz"
        "https://github.com/OpenVPN/easy-rsa/archive/3.0.1.tar.gz"
        'openvpn@.service')
md5sums=('3a1f3f63bdaede443b4df49957df9405'
         'aa58cbecda1d5ed6df6bb65e990e42a1'
         'c0275a1192a7e3c0a167a0dc4809dbaa')

build() {
  cd ${pkgname}-${pkgver}
  
  # Build openvpn
  CFLAGS="$CFLAGS -DPLUGIN_LIBDIR=\\\"/usr/lib/openvpn\\\""
     ./configure --prefix=/usr \
        --mandir=/usr/share/man \
        --enable-iproute2 \
        --enable-systemd
  make

  # Build easy-rsa
  #cd $srcdir/easy-rsa-3.0.1
  #autoreconf -i
  #./configure --prefix=/usr --with-easyrsadir=/usr/share/openvpn/easy-rsa
  #make
}

package() {
  cd ${pkgname}-${pkgver}
  # Install openvpn
  make DESTDIR=${pkgdir} install
  install -d -m755 ${pkgdir}/etc/openvpn/client
  install -d -m755 ${pkgdir}/etc/openvpn/server

  install -d -m755 ${pkgdir}/usr/share/openvpn
  cp -r sample/sample-config-files ${pkgdir}/usr/share/openvpn/examples

  install -D -m644 COPYING ${pkgdir}/usr/share/licenses/${pkgname}/COPYING

  install -d -m755 ${pkgdir}/usr/share/openvpn/contrib
  cp -r contrib ${pkgdir}/usr/share/openvpn
  
  # install systemd units
  install -Dm644 distro/systemd/openvpn-client@.service ${pkgdir}/usr/lib/systemd/system/openvpn-client@.service
  install -Dm644 distro/systemd/openvpn-server@.service ${pkgdir}/usr/lib/systemd/system/openvpn-server@.service
  
  # Install easy-rsa
  cd ${srcdir}/easy-rsa-3.0.1
  install -d ${pkgdir}/usr/share/doc/easy-rsa/
  mv doc/* ${pkgdir}/usr/share/doc/easy-rsa/
  rmdir doc
  install -d ${pkgdir}/usr/share/licenses/easy-rsa/
  mv COPYING ${pkgdir}/usr/share/licenses/easy-rsa/

  install -d ${pkgdir}/usr/share/easy-rsa/
  mv easyrsa3/* ${pkgdir}/usr/share/easy-rsa/
}
